<template>
  <div class="camera_page">
    <div class="camera_content">
      <div class="code_close" @click="closeClick()">
        <span>< 返回</span>
      </div>
      <div class="loop_line"></div>
      <div class="video_nav" >
        <video id="video_nav_id" autoplay :controls="false"></video>
      </div>
    </div>
  </div>
</template>

<script>
import adapter from 'webrtc-adapter';
import { BrowserMultiFormatReader } from "@zxing/library"
export default {
  data() {
    return {
      codeReader: null,
      videoInputDevices: [],
      deviceId: null,
    }
  },
  mounted() {
    // var video = document.getElementById('video_nav_id').getElementsByTagName('video')[0]
    var video = document.getElementById('video_nav_id')
    video.setAttribute('id','video_id')
    video.setAttribute('class','video_calss')
    this.initEvent()
  },
  methods: {
    initEvent() {
      this.codeReader = new BrowserMultiFormatReader();
      this.codeReader.listVideoInputDevices().then(res => {
        if(res.length > 0) {
          this.videoInputDevices = res
          this.deviceId = res[1].deviceId
          // alert(`deviceId:` + this.deviceId);
          console.log('res', res);
        }else{
          alert('当前没有可用视频通道');
        }
      }).catch(err => {
        // alert('当前浏览器环境不支持获取视频通道');
        console.log("扫码错误",err)
      })

      this.startScanning()
    },

    startScanning() {
      try {
        this.codeReader.decodeFromVideoDevice(this.deviceId, 'video_id',(res,err) => {
          if(res) {
            this.$emit('success', res)
            this.$router.replace("/stationFileDetail?code="+res)
            this.codeReader && this.codeReader.reset();
          }
        })
      }catch(err){
        alert(`初始化失败${err}`);
      }
    },

    closeClick() {
      // uni.navigateBack()
      this.$router.back()
    },

  }
}
</script>

<style scoped >
.camera_page {
  height: 100%;
  width: 100%;
}
.camera_content {
  height: 100%;
  width: 100%;
  position: relative;
}
.code_close {
  height: 50px;
  width: 100px;
  position: absolute;
  left: 30px;
  top: 30px;
  z-index: 999999;
  color: #fff;
  background: rgba(255,255,255,0.3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
}
.code_close > img {
  height: 100%;
  width: 100%;
  display: block;

}
.loop_line {
  height: 3px;
  width: 80%;
  background-color: aliceblue;
  border-radius: 50%;
  box-shadow: 1px -4px 25px 7px rgba(255, 255, 255, 0.5);
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  animation: myfirst 3s infinite;
  z-index: 999999;
}
@keyframes myfirst {
  10% {
    top: 20%;
  }
  50% {
    top: 80%;
  }
  100% {
    top: 20%;
  }
}
.video_nav {
  height: 100%;
  width: 100%;
  background: #000000;
  padding: 100px 0;
}
#video_nav_id {
  height: 100%;
  width: 100%;
}

</style>
